﻿var Employees = {}
Employees.__namespace = true;

Employees.init = function () {
    /// <summary>Initialize the page.</summary>

    var addButton = $("#employeesGrid .k-toolbar .k-grid-add");
    addButton.attr("class", "k-button")
             .attr("href", "#")
             .text("Add Employee")
             .bind("click", function (e) {
                 employee = {
                     EmployeeID: 0
                 };
                 Employees.editEmployee(employee);
             });
}

Employees.employeeGrid_onEdit = function (args) {
    /// <summary>Event handler when a the edit button is clicked for an employee.</summary>

    var employee = $("#employeesGrid").data("kendoGrid").dataSource.get(args.id);
    Employees.editEmployee(employee);
}

Employees.employeeGrid_onDelete = function (args) {
    /// <summary>Event handler when a the delete button is clicked for an employee.</summary>
    $.post("Employees/DeleteEmployee", { id: args.id }, function (data, textStatus, jqxhr) {
        if (data.success == true) {
            $('#employeesGrid').data('kendoGrid').dataSource.read();
        } else {
            alert(data.error);
        }
    });
}

Employees.editEmployee = function (employee) {
    EmployeeDetails.showWindow(employee, function (data) {
        // Save the employee.
        $.post("Employees/SaveEmployee", data, function (response, textStatus, jqxhr) {
            if (response.success == true) {
                $('#employeesGrid').data('kendoGrid').dataSource.read();
            } else {
                alert(response.error);
            }
        });
    });
}

var EmployeeDetails = {}
EmployeeDetails.__namespace = true;

EmployeeDetails._callback = null;
EmployeeDetails._employee = null;
EmployeeDetails._validator = null;

EmployeeDetails.showWindow = function (employee, callback) {
    /// <summary>Event handler when a grid row is selected.</summary>

    EmployeeDetails._employee = employee;
    EmployeeDetails._callback = callback;

    var $employeeDetailsWindow = $("#employeeDetailsWindow");

    $employeeDetailsWindow.find("#employeeDetailsWindow_Contents").load("Employees/EmployeeDetails?_=" + new Date().getTime(), function () {
        $employeeDetailsWindow.css("display", "");

        $employeeDetailsWindow.find("#id").val(employee.EmployeeID);
        $employeeDetailsWindow.find("#titleOfCourtesy").val(employee.TitleOfCourtesy);
        $employeeDetailsWindow.find("#title").val(employee.Title);
        $employeeDetailsWindow.find("#firstName").val(employee.FirstName);
        $employeeDetailsWindow.find("#lastName").val(employee.LastName);
        $employeeDetailsWindow.find("#extension").val(employee.Extension);
        $employeeDetailsWindow.find("#homePhone").val(employee.HomePhone);
        $employeeDetailsWindow.find("#address").val(employee.Address);
        $employeeDetailsWindow.find("#city").val(employee.City);
        $employeeDetailsWindow.find("#region").val(employee.Region);
        $employeeDetailsWindow.find("#country").val(employee.Country);
        $employeeDetailsWindow.find("#postalCode").val(employee.PostalCode);
        $employeeDetailsWindow.find("#birthDate").data("kendoDatePicker").value(employee.BirthDate);
        $employeeDetailsWindow.find("#hireDate").data("kendoDatePicker").value(employee.HireDate);

        var reportTo = $("#reportsTo").data("kendoDropDownList");
        reportTo.dataSource = $("#employeesGrid").data("kendoGrid").dataSource;
        reportTo.refresh();
        reportTo.value(employee.ReportsTo);

        EmployeeDetails._validator = $("#employeeForm").kendoValidator().data("kendoValidator");

        $employeeDetailsWindow.data("kendoWindow").open().center();
    });
}

EmployeeDetails.onButtonClick = function (sender) {
    /// <summary>Event handler when a button is clicked.</summary>

    var $button = $(sender);

    if ($button.text() == "OK") {
        /*
        try {
            EmployeeDetails._validator.validate();
        } finally {
            $.each($(".error-row"), function () {
                if ($.trim($(this).text()).length > 0) {
                    $(this).removeClass("hidden-row");
                } else {
                    $(this).addClass("hidden-row");
                }
            });
        }
        */
        if (EmployeeDetails._validator.validate()) {
            var $employeeDetailsWindow = $("#employeeDetailsWindow");
            var data = {
                "employee.EmployeeID": $employeeDetailsWindow.find("#id").val(),
                "employee.Title": $employeeDetailsWindow.find("#title").val(),
                "employee.TitleOfCourtesy": $employeeDetailsWindow.find("#titleOfCourtesy").val(),
                "employee.FirstName": $employeeDetailsWindow.find("#firstName").val(),
                "employee.LastName": $employeeDetailsWindow.find("#lastName").val(),
                "employee.Extension": $employeeDetailsWindow.find("#extension").val(),
                "employee.HomePhone": $employeeDetailsWindow.find("#homePhone").val(),
                "employee.Address": $employeeDetailsWindow.find("#address").val(),
                "employee.City": $employeeDetailsWindow.find("#city").val(),
                "employee.Region": $employeeDetailsWindow.find("#region").val(),
                "employee.Country": $employeeDetailsWindow.find("#country").val(),
                "employee.PostalCode": $employeeDetailsWindow.find("#postalCode").val(),
                "employee.BirthDate": $employeeDetailsWindow.find("#birthDate").data("kendoDatePicker").value().toJSON(),
                "employee.HireDate": $employeeDetailsWindow.find("#hireDate").data("kendoDatePicker").value().toJSON(),
                "employee.ReportsTo": $employeeDetailsWindow.find("#reportsTo").data("kendoDropDownList").value(),
            };
            $employeeDetailsWindow.data("kendoWindow").close();

            // Execute callback.
            EmployeeDetails._callback(data);
        } else {
            $.each($(".error-row"), function () {
                if ($.trim($(this).text()).length > 0) {
                    $(this).removeClass("hidden-row");
                } else {
                    $(this).addClass("hidden-row");
                }
            });
        }
    } else {
        var $employeeDetailsWindow = $("#employeeDetailsWindow");
        $employeeDetailsWindow.data("kendoWindow").close();
    }
}